<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns:schema_1_0="http://schemas.microsoft.com/ado/2006/04/edm"
    xmlns:schema_1_1="http://schemas.microsoft.com/ado/2007/05/edm"
    xmlns:schema_1_2="http://schemas.microsoft.com/ado/2008/09/edm"
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    
  	<xsl:output method="text"/>

  	<!-- Required fields for connecting to DB are passed externally -->
  	<xsl:param name="ServiceName"/>
  	<xsl:param name="DBName"/>
  	<xsl:param name="DBHost"/>
  	<xsl:param name="DBPort"/>
  	<xsl:param name="DBUser"/>
  	<xsl:param name="DBPass"/>  	  	  	  	
  	<xsl:param name="IDSQP_Version"/>
  	<xsl:param name="followPularizeSingularizeRule"/>  	
  	<xsl:template match="/">&lt;?php  	
 <!--
	/*
    Copyright 2011 Persistent Systems Limited
    Licensed under the New USD License scheme.;
   	you may not use this file except in compliance with the License.
   	You may obtain a copy of the License at

   	http://www.opensource.org/licenses/bsd-license.php

	*/

	/**
     * This code was generated by the tool 'MySQLConnector.php'.
     * Runtime Version:1.0
     *
     * Changes to this file may cause incorrect behavior and will be lost if
     * the code is regenerated.
     */
 -->
	<xsl:apply-templates select="/edmx:Edmx/edmx:DataServices/schema_1_0:Schema | /edmx:Edmx/edmx:DataServices/schema_1_1:Schema | /edmx:Edmx/edmx:DataServices/schema_1_2:Schema"/>
?&gt;
	</xsl:template>
	<xsl:template match="schema_1_0:Schema |schema_1_1:Schema |schema_1_2:Schema">
	
		<xsl:variable name="validEntitySetNameCond">
	        <xsl:for-each select="schema_1_0:EntityNameInformation |schema_1_1:EntityNameInformation |schema_1_2:EntityNameInformation">
       			<xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
       				<xsl:choose>
						<xsl:when test="@dbName = @EntitySetName"> 
        	and $resourceSetName !== '<xsl:value-of select="@EntitySetName"/>'
        				</xsl:when>
        				<xsl:otherwise>
	    	and $resourceSetName !== '<xsl:value-of select="@dbName"/>'
	        			</xsl:otherwise>
        			</xsl:choose>
   		     	</xsl:for-each>
       		</xsl:for-each>
		</xsl:variable>

		<xsl:variable name="modifyEntityNames">
	        <xsl:for-each select="schema_1_0:EntityNameInformation |schema_1_1:EntityNameInformation |schema_1_2:EntityNameInformation">
       			<xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
					<xsl:if test="@dbName != @EntitySetName"> 
        	if($resourceSetName === '<xsl:value-of select="@EntitySetName"/>')
        	{
        		$resourceSetName = '<xsl:value-of select="@dbName"/>';
        	}	
       				</xsl:if>
       			</xsl:for-each>
       		</xsl:for-each>
		</xsl:variable>       			

		<xsl:variable name="serializeEntitySetCases">
	        <xsl:for-each select="schema_1_0:EntityNameInformation |schema_1_1:EntityNameInformation |schema_1_2:EntityNameInformation">
       			<xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
				case '<xsl:value-of select="@dbName"/>':
	        		<!--  $returnResult = $this->_serialize<xsl:value-of select="translate(@dbName,' ','')"/>($stmt); -->
	        		$returnResult = $this->_serialize<xsl:value-of select="@EntitySetName"/>($stmt);
       				break;
				</xsl:for-each>
       		</xsl:for-each>	
		</xsl:variable>
		       			
		<xsl:variable name="serializeEntityTypeCases">
	        <xsl:for-each select="schema_1_0:EntityNameInformation |schema_1_1:EntityNameInformation |schema_1_2:EntityNameInformation">
       			<xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
				case '<xsl:value-of select="@dbName"/>':
	        		<!--  $returnResult = $this->_serialize<xsl:value-of select="translate(@EntityTypeName,'_','')"/>($stmt);-->
	        		$returnResult = $this->_serialize<xsl:value-of select="@EntityTypeName"/>($record);
       				break;
				</xsl:for-each>
       		</xsl:for-each>	
		</xsl:variable>
		
		<xsl:variable name="handleNavigationCondWithKey">
			<xsl:for-each select="schema_1_0:EntityType |schema_1_1:EntityType |schema_1_2:EntityType">
				<xsl:variable name="fromEntity">
					<xsl:value-of select="@Name"/>
				</xsl:variable>
				<!--  FromEntity:<xsl:value-of select="$fromEntity"/> -->
			else if($srcClass === '<xsl:value-of select="$fromEntity"/>')
			{		
				<xsl:variable name="entityNavigationSet">	
					<xsl:for-each select="schema_1_0:NavigationProperty |schema_1_1:NavigationProperty |schema_1_2:NavigationProperty">
						<xsl:variable name="navigationProperty">
							<xsl:value-of select="@Relationship"/>
						</xsl:variable>
						<!--  NavigationPropertyName:<xsl:value-of select="$navigationProperty"/> -->
						<xsl:variable name="fromRole">
							<xsl:value-of select="@FromRole"/>
						</xsl:variable>
						<!--  FromRole:<xsl:value-of select="$fromRole"/> -->
						<xsl:variable name="toRole">
							<xsl:value-of select="@ToRole"/>
						</xsl:variable>						
						<!--  ToRole:<xsl:value-of select="$toRole"/> -->
						<xsl:variable name="isToRoleExist">						
							<xsl:for-each select="//schema_1_0:EntityContainer | //schema_1_1:EntityContainer | //schema_1_2:EntityContainer">
								<xsl:for-each select="//schema_1_0:EntitySet | //schema_1_1:EntitySet | //schema_1_2:EntitySet">
									<xsl:if test="@Name=$toRole">1</xsl:if>
								</xsl:for-each>
							</xsl:for-each>
						</xsl:variable>
						<!--  IsToRoleExist:<xsl:value-of select="$isToRoleExist"/> -->
						<xsl:if test="$isToRoleExist=1">
							<xsl:variable name="isNavigationNeedToBeConsidered">
								<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">	
									<xsl:if test="@Name=$navigationProperty">
										<xsl:for-each select="schema_1_0:End | schema_1_1:End | schema_1_2:End">
											<xsl:if test="$toRole=@Role and @Multiplicity='*'">1</xsl:if>
										</xsl:for-each>
									</xsl:if>
								</xsl:for-each>									
							</xsl:variable>
							<xsl:if test="$isNavigationNeedToBeConsidered=1">							
							<xsl:variable name="fromTable">
								<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">	
									<xsl:if test="@Name=$navigationProperty">
										<xsl:for-each select="schema_1_0:ReferentialConstraint | schema_1_1:ReferentialConstraint | schema_1_2:ReferentialConstraint">
											<xsl:for-each select="schema_1_0:Principal | schema_1_1:Principal | schema_1_2:Principal">
												<xsl:value-of select="@Role"/>
											</xsl:for-each>
										</xsl:for-each>
									</xsl:if>
								</xsl:for-each>	
							</xsl:variable>
							<xsl:variable name="fromCond">
								<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">	
									<xsl:if test="@Name=$navigationProperty">
										<xsl:for-each select="schema_1_0:ReferentialConstraint | schema_1_1:ReferentialConstraint | schema_1_2:ReferentialConstraint">
											<xsl:for-each select="schema_1_0:Principal | schema_1_1:Principal | schema_1_2:Principal">
												<xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef | schema_1_2:PropertyRef"> 
													<xsl:variable name="toField">
														<xsl:value-of select="@Name"/>
													</xsl:variable> 
													<xsl:variable name="primaryKeyPos" select="position()"/>
													<xsl:for-each select="./../../schema_1_0:Dependent | ./../../schema_1_1:Dependent | ./../../schema_1_2:Dependent">
														<xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef | schema_1_2:PropertyRef">
														<xsl:if test="position() = $primaryKeyPos">
															and <xsl:value-of select="@Name"/> = '$sourceEntityInstance-><xsl:value-of select="$toField"/>'
														</xsl:if>
														</xsl:for-each>
													</xsl:for-each>																																		
												</xsl:for-each>												
											</xsl:for-each>
										</xsl:for-each>
									</xsl:if>
								</xsl:for-each>									
							</xsl:variable>
							<xsl:variable name="toTable">
								<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">	
									<xsl:if test="@Name=$navigationProperty">
										<xsl:for-each select="schema_1_0:ReferentialConstraint | schema_1_1:ReferentialConstraint | schema_1_2:ReferentialConstraint">														
											<xsl:for-each select="schema_1_0:Dependent | schema_1_1:Dependent | schema_1_2:Dependent">									
												<xsl:value-of select="@Role"/>
											</xsl:for-each>
										</xsl:for-each>
									</xsl:if>
								</xsl:for-each>
							</xsl:variable>	
							<xsl:if test="$toTable != ''">															
				else if($navigationPropName === '<xsl:value-of select="$toTable"/>') 
				{			
							<xsl:variable name="dbName_toTable"> 					
	       						<xsl:for-each select="//schema_1_0:EntityNameInformation | //schema_1_1:EntityNameInformation | //schema_1_2:EntityNameInformation">	
    								<xsl:for-each select="//schema_1_0:EntityName | //schema_1_1:EntityName | //schema_1_2:EntityName">
										<xsl:if test="$toTable = @EntitySetName or $toTable = @EntityTypeName"> 
											<xsl:value-of select="@dbName"/>
										</xsl:if>
   									</xsl:for-each>
       							</xsl:for-each>
							</xsl:variable>
					$query = "SELECT * FROM <xsl:value-of select="$dbName_toTable"/> WHERE <xsl:value-of select="substring-after(normalize-space($fromCond),'and ')"/> and $key";
			        $stmt = mysql_query($query);
        			if ($stmt === false) {            
        				die(print_r(mysql_error(), true));
	    			}
	    			$result = $this->_serialize<xsl:value-of select="$toTable"/>($stmt);
				}
							</xsl:if>
							</xsl:if>
						</xsl:if>
					</xsl:for-each>
				</xsl:variable>
				<xsl:if test="$entityNavigationSet != ''">
					<xsl:value-of select="substring-after($entityNavigationSet,'else ')"/>		
				else {
					die('<xsl:value-of select="$fromEntity"/> does not have navigation porperty with name: ' . $navigationPropName);
				}
				</xsl:if>
			}	
			</xsl:for-each>								
		</xsl:variable>		
  
		<xsl:variable name="handleNavigationCond">
			<xsl:for-each select="schema_1_0:EntityType |schema_1_1:EntityType |schema_1_2:EntityType">
				<xsl:variable name="fromEntity">
					<xsl:value-of select="@Name"/>
				</xsl:variable>
				<!--  FromEntity:<xsl:value-of select="$fromEntity"/> -->
			else if($srcClass === '<xsl:value-of select="$fromEntity"/>')
			{		
				<xsl:variable name="entityNavigationSet">	
					<xsl:for-each select="schema_1_0:NavigationProperty |schema_1_1:NavigationProperty |schema_1_2:NavigationProperty">
						<xsl:variable name="navigationProperty">
							<xsl:value-of select="@Relationship"/>
						</xsl:variable>
						<!--  NavigationPropertyName:<xsl:value-of select="$navigationProperty"/> -->
						<xsl:variable name="fromRole">
							<xsl:value-of select="@FromRole"/>
						</xsl:variable>
						<!--  FromRole:<xsl:value-of select="$fromRole"/> -->
						<xsl:variable name="toRole">
							<xsl:value-of select="@ToRole"/>
						</xsl:variable>						
						<!--  ToRole:<xsl:value-of select="$toRole"/> -->
						<xsl:variable name="isToRoleExist">						
							<xsl:for-each select="//schema_1_0:EntityContainer | //schema_1_1:EntityContainer | //schema_1_2:EntityContainer">
								<xsl:for-each select="//schema_1_0:EntitySet | //schema_1_1:EntitySet | //schema_1_2:EntitySet">
									<xsl:if test="@Name=$toRole">1</xsl:if>
								</xsl:for-each>
							</xsl:for-each>
						</xsl:variable>
						<!--  IsToRoleExist:<xsl:value-of select="$isToRoleExist"/> -->
						<xsl:if test="$isToRoleExist=1">
							<xsl:variable name="isNavigationNeedToBeConsidered">
								<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">	
									<xsl:if test="@Name=$navigationProperty">
										<xsl:for-each select="schema_1_0:End | schema_1_1:End | schema_1_2:End">
											<xsl:if test="$toRole=@Role and @Multiplicity='*'">1</xsl:if>
										</xsl:for-each>
									</xsl:if>
								</xsl:for-each>									
							</xsl:variable>
							<xsl:if test="$isNavigationNeedToBeConsidered=1">
							<xsl:variable name="fromTable">
								<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">	
									<xsl:if test="@Name=$navigationProperty">
										<xsl:for-each select="schema_1_0:ReferentialConstraint | schema_1_1:ReferentialConstraint | schema_1_2:ReferentialConstraint">
											<xsl:for-each select="schema_1_0:Principal | schema_1_1:Principal | schema_1_2:Principal">
												<xsl:value-of select="@Role"/>
											</xsl:for-each>
										</xsl:for-each>
									</xsl:if>
								</xsl:for-each>	
							</xsl:variable>
							<xsl:variable name="fromCond">
								<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">	
									<xsl:if test="@Name=$navigationProperty">
										<xsl:for-each select="schema_1_0:ReferentialConstraint | schema_1_1:ReferentialConstraint | schema_1_2:ReferentialConstraint">
											<xsl:for-each select="schema_1_0:Principal | schema_1_1:Principal | schema_1_2:Principal">						
												<xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef | schema_1_2:PropertyRef"> 
													<xsl:variable name="toField">
														<xsl:value-of select="@Name"/>
													</xsl:variable> 
													<xsl:variable name="primaryKeyPos" select="position()"/>
													<xsl:for-each select="./../../schema_1_0:Dependent | ./../../schema_1_1:Dependent | ./../../schema_1_2:Dependent">
														<xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef | schema_1_2:PropertyRef">
														<xsl:if test="position() = $primaryKeyPos">
															and <xsl:value-of select="@Name"/> = '$sourceEntityInstance-><xsl:value-of select="$toField"/>'
														</xsl:if>
														</xsl:for-each>
													</xsl:for-each>
												</xsl:for-each>
											</xsl:for-each>
										</xsl:for-each>
									</xsl:if>
								</xsl:for-each>		
							</xsl:variable>
							<xsl:variable name="toTable">
								<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">	
									<xsl:if test="@Name=$navigationProperty">
										<xsl:for-each select="schema_1_0:ReferentialConstraint | schema_1_1:ReferentialConstraint | schema_1_2:ReferentialConstraint">														
											<xsl:for-each select="schema_1_0:Dependent | schema_1_1:Dependent | schema_1_2:Dependent">									
												<xsl:value-of select="@Role"/>
											</xsl:for-each>
										</xsl:for-each>
									</xsl:if>
								</xsl:for-each>
							</xsl:variable>	
							<xsl:if test="$toTable != ''">															
				else if($navigationPropName === '<xsl:value-of select="$toTable"/>') 
				{			
							<xsl:variable name="dbName_toTable"> 					
	       						<xsl:for-each select="//schema_1_0:EntityNameInformation | //schema_1_1:EntityNameInformation | //schema_1_2:EntityNameInformation">	
    								<xsl:for-each select="//schema_1_0:EntityName | //schema_1_1:EntityName | //schema_1_2:EntityName">
										<xsl:if test="$toTable = @EntitySetName or $toTable = @EntityTypeName"> 
											<xsl:value-of select="@dbName"/>
										</xsl:if>
   									</xsl:for-each>
       							</xsl:for-each>
							</xsl:variable>
					$query = "SELECT * FROM <xsl:value-of select="$dbName_toTable"/> WHERE <xsl:value-of select="substring-after(normalize-space($fromCond),'and ')"/>";
			        $stmt = mysql_query($query);
        			if ($stmt === false) {            
        				die(print_r(mysql_error(), true));
	    			}
	    			$result = $this->_serialize<xsl:value-of select="$toTable"/>($stmt);
				}
							</xsl:if>
							</xsl:if>	
						</xsl:if>
					</xsl:for-each>
				</xsl:variable>
				<xsl:if test="$entityNavigationSet != ''">
					<xsl:value-of select="substring-after($entityNavigationSet,'else ')"/>		
				else {
					die('<xsl:value-of select="$fromEntity"/> does not have navigation porperty with name: ' . $navigationPropName);
				}
				</xsl:if>
			}	
			</xsl:for-each>										
   		</xsl:variable>       		
		       			  		       			
		<xsl:variable name="handleNavigationCondWithNull">
			<xsl:for-each select="schema_1_0:EntityType |schema_1_1:EntityType |schema_1_2:EntityType">
				<xsl:variable name="fromEntity">
					<xsl:value-of select="@Name"/>
				</xsl:variable>
			else if($srcClass==='<xsl:value-of select="$fromEntity"/>')
			{
					<xsl:variable name="fromEntitySetName">
			       		<xsl:for-each select="//schema_1_0:EntityNameInformation | //schema_1_1:EntityNameInformation | //schema_1_2:EntityNameInformation">	
    						<xsl:for-each select="//schema_1_0:EntityName | //schema_1_1:EntityName | //schema_1_2:EntityName">
								<xsl:if test="$fromEntity = @EntityTypeName"> 
									<xsl:value-of select="@EntitySetName"/>
								</xsl:if>
   							</xsl:for-each>
       					</xsl:for-each>														
					</xsl:variable>    
					<xsl:variable name="entityReferenceNevigations">
						<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">
							<xsl:variable name="navigationName"><xsl:value-of select="@Name"/></xsl:variable>
							<xsl:variable name="fromNavigationFound">
								<xsl:for-each select="schema_1_0:End | schema_1_1:End | schema_1_2:End">
									<xsl:if test="$fromEntitySetName = @Role and @Multiplicity='*'">1</xsl:if>
								</xsl:for-each>
							</xsl:variable>
							<xsl:variable name="toNavigation">
								<xsl:if test="$fromNavigationFound=1">
									<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">
										<xsl:if test="@Name=$navigationName">
											<xsl:for-each select="schema_1_0:End | schema_1_1:End | schema_1_2:End">
												<xsl:if test="@Role != $fromEntity and @Multiplicity=1">
													<xsl:value-of select="@Role"/>
												</xsl:if>	
											</xsl:for-each>
										</xsl:if>
									</xsl:for-each>
								</xsl:if>
							</xsl:variable>							
							<xsl:if test="$fromNavigationFound = 1  and $toNavigation != ''">
								<xsl:variable name="toNavigationFields">
									<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">
										<xsl:if test="@Name=$navigationName">
											<xsl:for-each select="schema_1_0:ReferentialConstraint | schema_1_1:ReferentialConstraint | schema_1_2:ReferentialConstraint">
												<xsl:for-each select="schema_1_0:Dependent | schema_1_1:Dependent | schema_1_2:Dependent">
													<xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef | schema_1_2:PropertyRef">
														and $sourceEntityInstance-><xsl:value-of select="@Name"/>
													</xsl:for-each>
												</xsl:for-each>
											</xsl:for-each>
										</xsl:if>
									</xsl:for-each>								
								</xsl:variable>
								<xsl:variable name="navigationCond">
									<xsl:for-each select="//schema_1_0:Association | //schema_1_1:Association | //schema_1_2:Association">
										<xsl:if test="@Name=$navigationName">
											<xsl:for-each select="schema_1_0:ReferentialConstraint | schema_1_1:ReferentialConstraint | schema_1_2:ReferentialConstraint">
												<xsl:for-each select="schema_1_0:Principal | schema_1_1:Principal | schema_1_2:Principal">						
													<xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef | schema_1_2:PropertyRef"> 
														<xsl:variable name="fromField">
															<xsl:value-of select="@Name"/>
														</xsl:variable> 
														<xsl:variable name="primaryKeyPos" select="position()"/>
														<xsl:for-each select="./../../schema_1_0:Dependent | ./../../schema_1_1:Dependent | ./../../schema_1_2:Dependent">
															<xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef | schema_1_2:PropertyRef">
																<xsl:if test="position() = $primaryKeyPos">
																	and <xsl:value-of select="$fromField"/> = '$sourceEntityInstance-><xsl:value-of select="@Name"/>'
																</xsl:if>
															</xsl:for-each>
														</xsl:for-each>
													</xsl:for-each>
												</xsl:for-each>
											</xsl:for-each>
										</xsl:if>
									</xsl:for-each>
								</xsl:variable>
								<xsl:if test="$navigationCond != ''">
									<xsl:variable name="toTableNameInDB">
			       						<xsl:for-each select="//schema_1_0:EntityNameInformation | //schema_1_1:EntityNameInformation | //schema_1_2:EntityNameInformation">	
    										<xsl:for-each select="//schema_1_0:EntityName | //schema_1_1:EntityName | //schema_1_2:EntityName">
												<xsl:if test="$toNavigation = @EntitySetName or $toNavigation = @EntityTypeName"> 
													<xsl:value-of select="@dbName"/>
												</xsl:if>
   											</xsl:for-each>
       									</xsl:for-each>									
									</xsl:variable>
				else if($navigationPropName === '<xsl:value-of select="$toNavigation"/>')
				{
					if (empty(<xsl:value-of select="substring-after(normalize-space($toNavigationFields),'and ')"/>)) 
					{
                		$result = null;
					} else {
						$query = "SELECT * FROM <xsl:value-of select="$toTableNameInDB"/> WHERE <xsl:value-of select="substring-after(normalize-space($navigationCond),'and ')"/>";
						$stmt = mysql_query($query);
						if ($stmt === false) {
							die(print_r(mysql_error(), true));
						}
						if (!mysql_num_rows($stmt)) {
							$result =  null;
						}
						$result = $this->_serialize<xsl:value-of select="$toNavigation"/>(mysql_fetch_array($stmt, MYSQL_ASSOC));
					}
				}
								</xsl:if>			
							</xsl:if>
						</xsl:for-each>
					</xsl:variable>
					<xsl:value-of select="substring-after($entityReferenceNevigations,'else')"/>
						<xsl:if test="$entityReferenceNevigations != ''">
				else {
					die('<xsl:value-of select="$fromEntity"/> does not have navigation porperty with name: ' . $navigationPropName);
				}
						</xsl:if>					
			}
				</xsl:for-each>
		</xsl:variable>
	/** 
	 * Implementation of IDataServiceQueryProvider.
	 * 
	 * PHP version 5.3
	 * 
	 * @category  Service
	 * @package   <xsl:value-of select="@Namespace"/>;
	 * @author    MySQLConnector &lt;odataphpproducer_alias@microsoft.com&gt;
	 * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
	 * @license   New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
	 * @version   SVN: 1.0
	 * @link      http://odataphpproducer.codeplex.com
	 */     
	use ODataProducer\UriProcessor\ResourcePathProcessor\SegmentParser\KeyDescriptor;
	use ODataProducer\Providers\Metadata\ResourceSet;
	use ODataProducer\Providers\Metadata\ResourceProperty;
	use ODataProducer\Providers\Query\IDataServiceQueryProvider;
	require_once "<xsl:value-of select="@Namespace"/>Metadata.php";
	require_once "ODataProducer/Providers/Query/IDataServiceQueryProvider.php";
	
	/** The name of the database for <xsl:value-of select="@Namespace"/>*/
	define('DB_NAME', "<xsl:value-of select="$DBName"/>");
	/** MySQL database username */
	define('DB_USER', "<xsl:value-of select="$DBUser"/>");
	/** MySQL database password */
	define('DB_PASSWORD', "<xsl:value-of select="$DBPass"/>");
	/** MySQL hostname */
	define('DB_HOST', "<xsl:value-of select="$DBHost"/>");
			
   			
	/**
     * <xsl:value-of select="@Namespace"/>QueryProvider implemetation of IDataServiceQueryProvider.
	 * @category  Service
	 * @package   <xsl:value-of select="@Namespace"/>;
	 * @author    MySQLConnector &lt;odataphpproducer_alias@microsoft.com&gt;
	 * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
	 * @license   New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
	 * @version   Release: 1.0
	 * @link      http://odataphpproducer.codeplex.com
	 */
	class <xsl:value-of select="@Namespace"/>QueryProvider implements IDataServiceQueryProvider
	{
    	/**
     	 * Handle to connection to Database     
     	 */
    	private $_connectionHandle = null;

    	/**
     	 * Constructs a new instance of <xsl:value-of select="@Namespace"/>QueryProvider
     	 * 
     	 */
	    public function __construct()
    	{
        	$this->_connectionHandle = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, true);
        	if ( $this->_connectionHandle ) {
        		mysql_select_db(DB_NAME, $this->_connectionHandle);
        	} else {             
            	die(mysql_error());
        	} 
    	}

	    /**
    	 * Gets collection of entities belongs to an entity set
     	 * 
     	 * @param ResourceSet $resourceSet The entity set whose entities needs to be fetched
     	 * 
     	 * @return array(Object)
     	 */
    	public function getResourceSet(ResourceSet $resourceSet)
    	{   
        	$resourceSetName =  $resourceSet->getName();
			if(<xsl:value-of select="substring-after($validEntitySetNameCond,'and')"/>)	       		
        	{
        		die('(<xsl:value-of select="@Namespace"/>QueryProvider) Unknown resource set ' . $resourceSetName);
        	}
			<xsl:value-of select="$modifyEntityNames"/>        	
        	$query = "SELECT * FROM $resourceSetName";
        	$stmt = mysql_query($query);
        	if ($stmt === false) {
            	die(print_r(mysql_error(), true));
        	}

        	$returnResult = array();
        	switch ($resourceSetName) {
        		<xsl:value-of select="$serializeEntitySetCases"/>
        	}
        	mysql_free_result($stmt);
        	return $returnResult;        
		} 


	    /**
    	 * Gets an entity instance from an entity set identifed by a key
	     * 
    	 * @param ResourceSet   $resourceSet   The entity set from which 
	     *                                     an entity needs to be fetched
    	 * @param KeyDescriptor $keyDescriptor The key to identify the entity to be fetched
     	 * 
	     * @return Object/NULL Returns entity instance if found else null
    	 */
	    public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor)
    	{   
        	$resourceSetName =  $resourceSet->getName();
        	if(<xsl:value-of select="substring-after($validEntitySetNameCond,'and')"/>)	       		
        	{
	        	die('(<xsl:value-of select="@Namespace"/>QueryProvider) Unknown resource set ' . $resourceSetName);
    	    }
    	    <xsl:value-of select="$modifyEntityNames"/>
    	
        	$namedKeyValues = $keyDescriptor->getValidatedNamedValues();
        	$condition = null;
        	foreach ($namedKeyValues as $key => $value) {
	            $condition .= $key . ' = ' . $value[0] . ' and ';
    	    }
	
    	    $len = strlen($condition);
        	$condition = substr($condition, 0, $len - 5); 
	        $query = "SELECT * FROM $resourceSetName WHERE $condition";
    	    $stmt = mysql_query($query);
        	if ($stmt === false) {
            	die(print_r(mysql_error(), true));
        	}

        	//If resource not found return null to the library
        	if (!mysql_num_rows($stmt)) {
            	return null;
        	}

	        $result = null;
        	while ( $record = mysql_fetch_array($stmt, MYSQL_ASSOC)) {
    	    	switch ($resourceSetName) {
    	    		<xsl:value-of select="$serializeEntityTypeCases"/>
        		}
        	}	
        	mysql_free_result($stmt);
        	return $returnResult;        
    	}
    	
	    /**
    	 * Gets a related entity instance from an entity set identifed by a key
	     * 
    	 * @param ResourceSet      $sourceResourceSet    The entity set related to
	     *                                               the entity to be fetched.
    	 * @param object           $sourceEntityInstance The related entity instance.
     	 * @param ResourceSet      $targetResourceSet    The entity set from which
     	 *                                               entity needs to be fetched.
     	 * @param ResourceProperty $targetProperty       The metadata of the target 
     	 *                                               property.
     	 * @param KeyDescriptor    $keyDescriptor        The key to identify the entity 
     	 *                                               to be fetched.
     	 * 
     	 * @return Object/NULL Returns entity instance if found else null
     	 */
    	public function  getResourceFromRelatedResourceSet(ResourceSet $sourceResourceSet, 
        	$sourceEntityInstance, 
        	ResourceSet $targetResourceSet,
        	ResourceProperty $targetProperty,
        	KeyDescriptor $keyDescriptor
    	) {
        	$result = array();
        	$srcClass = get_class($sourceEntityInstance);
        	$navigationPropName = $targetProperty->getName();
        	$key = null;
        	foreach ($keyDescriptor->getValidatedNamedValues() as $keyName => $valueDescription) {
	            $key = $key . $keyName . '=' . $valueDescription[0] . ' and ';
    	    }
        	$key = rtrim($key, ' and ');
       		<xsl:value-of select="substring-after($handleNavigationCondWithKey,'else ')"/>
       		return empty($result) ? null : $result[0];	
		}
		  
	    /**
    	 * Get related resource set for a resource
     	* 
     	* @param ResourceSet      $sourceResourceSet    The source resource set
     	* @param mixed            $sourceEntityInstance The resource
     	* @param ResourceSet      $targetResourceSet    The resource set of 
     	*                                               the navigation property
     	* @param ResourceProperty $targetProperty       The navigation property to be 
     	*                                               retrieved
     	*                                               
     	* @return array(Objects)/array() Array of related resource if exists, if no 
     	*                                related resources found returns empty array
     	*/
    	public function  getRelatedResourceSet(ResourceSet $sourceResourceSet, 
        	$sourceEntityInstance, 
        	ResourceSet $targetResourceSet,
        	ResourceProperty $targetProperty
    	) {
	        $result = array();
    	    $srcClass = get_class($sourceEntityInstance);
	        $navigationPropName = $targetProperty->getName();
       		<xsl:value-of select="substring-after($handleNavigationCond,'else ')"/>
       		return $result;	        
    	}    
    	
	    /**
    	 * Get related resource for a resource
     	* 
     	* @param ResourceSet      $sourceResourceSet    The source resource set
     	* @param mixed            $sourceEntityInstance The source resource
     	* @param ResourceSet      $targetResourceSet    The resource set of 
     	*                                               the navigation property
     	* @param ResourceProperty $targetProperty       The navigation property to be 
     	*                                               retrieved
     	* 
     	* @return Object/null The related resource if exists else null
     	*/
    	public function getRelatedResourceReference(ResourceSet $sourceResourceSet, 
        	$sourceEntityInstance, 
        	ResourceSet $targetResourceSet,
        	ResourceProperty $targetProperty
    	) {
        	$result = null;
        	$srcClass = get_class($sourceEntityInstance);
        	$navigationPropName = $targetProperty->getName();
			<xsl:value-of select="substring-after($handleNavigationCondWithNull,'else ')"/>
			return $result;
		}
			
		<xsl:for-each select="schema_1_0:EntityNameInformation | schema_1_1:EntityNameInformation | schema_1_2:EntityNameInformation">
			<xsl:for-each select="schema_1_0:EntityName | schema_1_1:EntityName | schema_1_2:EntityName">
				<xsl:variable name="EntityNameInDB"> 					
					<xsl:value-of select="@dbName"/>
				</xsl:variable>			
				<xsl:variable name="EntitySetName"> 					
					<xsl:value-of select="@EntitySetName"/>
				</xsl:variable>
				<xsl:variable name="EntityTypeName">
					<xsl:value-of select="@EntityTypeName"/>
				</xsl:variable>
		/**
    	 * Serialize the sql result array into <xsl:value-of select="$EntityTypeName"/> objects
		 * 	
     	 * @param array(array) $result result of the sql query
     	 * 
     	 * @return array(Object)
     	 */
    	private function _serialize<xsl:value-of select="$EntitySetName"/>($result)
    	{
        	$<xsl:value-of select="$EntitySetName"/> = array();
        	while ($record = mysql_fetch_array($result, MYSQL_ASSOC)) {         
            	$<xsl:value-of select="$EntitySetName"/>[] = $this->_serialize<xsl:value-of select="$EntityTypeName"/>($record);
        	}
        	return $<xsl:value-of select="$EntitySetName"/>;
    	}
    	
    	/**
    	 * Serialize the sql row into <xsl:value-of select="$EntityTypeName"/> object
	     * 
    	 * @param array $record each row of <xsl:value-of select="$EntityTypeName"/>
	     * 
    	 * @return Object
	     */
	    private function _serialize<xsl:value-of select="$EntityTypeName"/>($record)
    	{
        	$<xsl:value-of select="$EntityTypeName"/> = new <xsl:value-of select="$EntityTypeName"/>();
        	<xsl:for-each select="//schema_1_0:MappingDetails | //schema_1_1:MappingDetails | //schema_1_2:MappingDetails">
        		<xsl:for-each select="schema_1_0:MapEntity | schema_1_1:MapEntity | schema_1_2:MapEntity">	
					<xsl:variable name="mappedEntityName">
						<xsl:value-of select="@UserEntityName"/>
					</xsl:variable>
	       			<xsl:if test="$EntityTypeName=$mappedEntityName or $EntitySetName=$mappedEntityName">
	       				<xsl:for-each select="schema_1_0:MapProperty | schema_1_1:MapProperty | schema_1_2:MapProperty">
	       					<xsl:variable name="userPropertyName">
	       						<xsl:value-of select="@UserPropertyName"/>
	       					</xsl:variable>
	       					<xsl:variable name="dbPropertyName">
	       						<xsl:value-of select="@DBPropertyName"/>
	       					</xsl:variable>
	       					<xsl:variable name="propertyType">
		       					<xsl:for-each select="//schema_1_0:EntityType | //schema_1_1:EntityType | //schema_1_2:EntityType">
	    	   						<xsl:if test="@Name = $EntityTypeName">
	    	   							<xsl:for-each select="//schema_1_0:Property | //schema_1_1:Property | //schema_1_2:Property">
	    	   								<xsl:if test="@Name = $dbPropertyName">
	    	   									<xsl:value-of select="@Type"/>
	    	   								</xsl:if>
	    	   							</xsl:for-each>
	       							</xsl:if>
	       						</xsl:for-each>
	       					</xsl:variable>
							<xsl:choose>
								<xsl:when test="$propertyType='Edm.Time'">
			$<xsl:value-of select="$EntityTypeName"/>-><xsl:value-of select="$userPropertyName"/> =  !is_null($record['<xsl:value-of select="$dbPropertyName"/>']) ? $record['<xsl:value-of select="$dbPropertyName"/>']->format('Y-m-d\TH:i:s') : null;							
								</xsl:when>
								<xsl:otherwise>
			$<xsl:value-of select="$EntityTypeName"/>-><xsl:value-of select="$userPropertyName"/> = $record['<xsl:value-of select="$dbPropertyName"/>'];							
								</xsl:otherwise>
							</xsl:choose>
	       				</xsl:for-each>
	       			</xsl:if>
	       		</xsl:for-each>				       		
    		</xsl:for-each>
    		return $<xsl:value-of select="$EntityTypeName"/>;
		}										
			</xsl:for-each>
		</xsl:for-each>
	    /**
    	 * The destructor     
     	 */
    	public function __destruct()
    	{
        	if ($this->_connectionHandle) {
            	mysql_close($this->_connectionHandle);
        	}
    	}		
    }

	</xsl:template>	   			
</xsl:stylesheet>